{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.022675Z",
     "start_time": "2024-12-19T03:56:51.005582Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression  # 导入线性回归模型\n",
    "import matplotlib.pyplot as plt"
   ],
   "execution_count": 72,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.038201Z",
     "start_time": "2024-12-19T03:56:51.025195Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X1 = 2 * np.random.rand(100, 1)  # 随机生成 100 个 1 维数据\n",
    "X2 = 2 * np.random.rand(100, 1)  # 随机生成 100 个 1 维数据"
   ],
   "id": "237f1016250e3a38",
   "execution_count": 73,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.053292Z",
     "start_time": "2024-12-19T03:56:51.040201Z"
    }
   },
   "cell_type": "code",
   "source": "X1",
   "id": "98a4e3ba78e726f0",
   "execution_count": 74,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.068891Z",
     "start_time": "2024-12-19T03:56:51.054811Z"
    }
   },
   "cell_type": "code",
   "source": "X2",
   "id": "cd003fca6159b8fc",
   "execution_count": 75,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.084421Z",
     "start_time": "2024-12-19T03:56:51.071407Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.c_ 将 X1 和 X2 沿着列方向堆叠在一起，形成一个 (100, 2) 的矩阵 X。\n",
    "X = np.c_[X1, X2]\n",
    "X\n",
    "# 样本数据"
   ],
   "id": "636f36f50269494c",
   "execution_count": 76,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.100406Z",
     "start_time": "2024-12-19T03:56:51.086407Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "4 是截距项。\n",
    "3 * X1 和 5 * X2 是特征 X1 和 X2 的系数。\n",
    "np.random.randn(100, 1) 添加一些高斯噪声，使数据更加真实。\n",
    "\"\"\"\n",
    "y = 4 + 3 * X1 + 5 * X2 + np.random.randn(100, 1)\n",
    "y"
   ],
   "id": "4e9f5a69314f1d1f",
   "execution_count": 77,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.116423Z",
     "start_time": "2024-12-19T03:56:51.102408Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建线性回归模型 fit_intercept=True 表示拟合截距项\n",
    "reg = LinearRegression(fit_intercept=True)\n",
    "\n",
    "# reg.fit(X, y) 使用训练数据 X 和目标变量 y 来训练模型\n",
    "reg.fit(X, y)\n",
    "\n",
    "# 打印模型的截距项和系数\n",
    "# Intercept 截距\n",
    "# Coefficient 系数\n",
    "print(reg.intercept_, reg.coef_)"
   ],
   "id": "5241bd1763d3d865",
   "execution_count": 78,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T03:56:51.132407Z",
     "start_time": "2024-12-19T03:56:51.119409Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 输入测试数据，包含三个样本。\n",
    "X_new = np.array([[0, 0],\n",
    "                  [2, 1],\n",
    "                  [2, 4]]) \n",
    "\n",
    "\n",
    "# 使用训练好的模型对新数据进行预测，得到预测值 y_predict。\n",
    "y_predict = reg.predict(X_new)\n",
    "y_predict"
   ],
   "id": "b1ab2fdb7293911",
   "execution_count": 79,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T06:12:46.586342Z",
     "start_time": "2024-12-19T06:12:46.559344Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(X_new[:, 0])\n",
    "print(X_new[:, 1])"
   ],
   "id": "b0b76219ff7d1963",
   "execution_count": 86,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-19T06:21:53.653301Z",
     "start_time": "2024-12-19T06:21:53.554416Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.plot(X_new[:, 0], y_predict, 'r-') # 红色实线 ('r-') \n",
    "plt.plot(X_new[:, 0], y_predict, 'r.') #  红色点 '.'\n",
    "plt.plot(X1, y, 'b.') # 蓝色点 '.'\n",
    "\n",
    "\n",
    "plt.plot(X_new[:, 1], y_predict, 'y-') # 红色实线 ('r-') \n",
    "plt.plot(X_new[:, 1], y_predict, 'y.') # 红色点 '.'\n",
    "plt.plot(X2, y, 'g.') # 绿色点 '.'\n",
    "\n",
    "# plt.axis([0, 2, 0, 25])\n",
    "plt.axis([0, 10, 0, 45])\n",
    "\n",
    " \n",
    "plt.show()\n"
   ],
   "id": "1e4f3a17e9146d2e",
   "execution_count": 120,
   "outputs": []
  },
  {
   "metadata": {},
   "cell_type": "code",
   "execution_count": null,
   "source": "",
   "id": "d1a15b6a1ffc07c4",
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
